﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using HelperLibrary;
using MasterLibrary.Cryptography;
using MasterLibrary.Net.Mail;
using MasterLibrary.Windows.Forms;
using MasterLibrary.Windows.Forms.Aero;

namespace Prometheus
{
    public partial class wzSettingsGlobal : WizardPage
    {
        public event EventHandler SearchForUpdates;
        private void OnSearchForUpdates()
        {
            EventHandler ev = SearchForUpdates;
            if (ev != null) ev(this, EventArgs.Empty);
        }

        public wzSettingsGlobal()
        {
            InitializeComponent();
            
            this.ReloadConfiguration();
        }

        private void ReloadConfiguration()
        {
            this.chkAutorun.Checked = Globals.appConfig.Autostart;
            this.chkEmptyFolder.Checked = Globals.appConfig.DeleteEmptyFolder;
            this.chkTray.Checked = Globals.appConfig.StartSysTray;

            this.txtMail.Text = Globals.appConfig.EmailAddress;
            try
            {
                this.txtPass.Text = RijndaelClass.DecryptString(Globals.appConfig.EmailPassword, "prometheus_cryptolib");
            }
            catch { }
            this.txtSmtpServer.Text = Globals.appConfig.EmailSMTP;
            this.txtUser.Text = Globals.appConfig.EmailUsername;
            this.txtDestination.Text = Globals.appConfig.DestinationPath;
            this.chkCompress.Checked = Globals.appConfig.UseCompression;
            this.chkCrypt.Checked = Globals.appConfig.UseCryptography;
            try
            {
                this.txtCryptPass.Text = RijndaelClass.DecryptString(Globals.appConfig.CryptPass, "prometheus_cryptolib");
            }
            catch { }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            this.OnSearchForUpdates();
        }

        private void btnTestMail_Click(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(SendTestMail)).Start();
        }

        private void SendTestMail()
        {
            SmtpMailer Mail = new SmtpMailer();

            Mail.UseCredentials = true;
            Mail.SmtpHost = txtSmtpServer.Text;
            Mail.SmtpUser = txtUser.Text;
            Mail.SmtpPass = txtPass.Text;

            Mail.Recipients.Add(txtMail.Text);
            Mail.SenderAddress = "monitoring@camelot-itlab.com";

            Mail.Subject = "Prometheus: Mail Settings Test";
            Mail.Message = "Sie haben die Einstellungen korrekt vorgenommen.";

            try
            {
                Mail.Send();
                ExtMessageBox.Show(null, "Prometheus", "E-Mail wurde verschickt", "Bitte prüfen Sie nun Ihren Posteingang.", MessageBoxButtons.OK, ExtMessageBoxIcon.None);
            }
            catch
            {
                ExtMessageBox.Show(null, "Es ist ein Fehler aufgetreten", "Falsche E-Mail-Einstellungen", "Bitte überprüfen Sie die angegebenen E-Mail-Einstellungen.", MessageBoxButtons.OK, ExtMessageBoxIcon.Error);
            }
        }

        private void chkAutorun_CheckedChanged(object sender, EventArgs e)
        {
            Globals.appConfig.Autostart = chkAutorun.Checked;
            MasterLibrary.Windows.SystemMethods.SetWindowsAutorun("Prometheus Autostart", Application.StartupPath + @"\" + "Prometheus.exe", chkAutorun.Checked);
            Globals.SaveConfiguration(Globals.appConfig);
        }

        private void chkTray_CheckedChanged(object sender, EventArgs e)
        {
            Globals.appConfig.StartSysTray = chkTray.Checked;
            Globals.SaveConfiguration(Globals.appConfig);
        }

        private void chkEmptyFolder_CheckedChanged(object sender, EventArgs e)
        {
            Globals.appConfig.DeleteEmptyFolder = chkEmptyFolder.Checked;
            Globals.SaveConfiguration(Globals.appConfig);
        }

        private void txtMail_Leave(object sender, EventArgs e)
        {
            Globals.appConfig.EmailAddress = txtMail.Text;
            Globals.SaveConfiguration(Globals.appConfig);
        }

        private void txtSmtpServer_Leave(object sender, EventArgs e)
        {
            Globals.appConfig.EmailSMTP = txtSmtpServer.Text;
            Globals.SaveConfiguration(Globals.appConfig);
        }

        private void txtUser_Leave(object sender, EventArgs e)
        {
            Globals.appConfig.EmailUsername = txtUser.Text;
            Globals.SaveConfiguration(Globals.appConfig);
        }

        private void txtPass_Leave(object sender, EventArgs e)
        {
            Globals.appConfig.EmailPassword = RijndaelClass.EncryptString(txtPass.Text, "prometheus_cryptolib");
            Globals.SaveConfiguration(Globals.appConfig);
        }

        private void btnSearchSource_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                this.txtDestination.Text = folderBrowserDialog.SelectedPath;
                Globals.appConfig.DestinationPath = this.txtDestination.Text;
                Globals.SaveConfiguration(Globals.appConfig);
            }
        }

        private void txtDestination_TextChanged(object sender, EventArgs e)
        {
            Globals.appConfig.DestinationPath = this.txtDestination.Text;
            Globals.SaveConfiguration(Globals.appConfig);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.txtCryptPass.Enabled = chkCrypt.Checked;
            Globals.appConfig.UseCryptography = chkCrypt.Checked;
            Globals.SaveConfiguration(Globals.appConfig);
        }

        private void chkCompress_CheckedChanged(object sender, EventArgs e)
        {
            Globals.appConfig.UseCompression = chkCompress.Checked;
            Globals.SaveConfiguration(Globals.appConfig);
        }

        private void txtCryptPass_Leave(object sender, EventArgs e)
        {
            Globals.appConfig.CryptPass = RijndaelClass.EncryptString(this.txtCryptPass.Text, "prometheus_cryptolib");
            Globals.SaveConfiguration(Globals.appConfig);
        }
    }
}
